package org.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.example.dao.UserDao;
import org.example.entity.User;
import org.example.entity.vo.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * Copyright: Copyright (c) 2022 Asiainfo
 *
 * @ClassName: org.example.service.impl.UserDetailsServiceImpl
 * @Description: 该类的功能描述
 * @version: v1.0.0
 * @author: 炫沙糖桔的鹏仔
 * @date: 2022/8/9 11:34
 * <p>
 * Modification History:
 * Date         Author          Version            Description
 * ------------------------------------------------------------
 * 2022/8/9      炫沙糖桔的鹏仔          v1.0.0               修改原因
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {


    @Autowired
    UserDao userDao;
    /**
     * @param username
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //        查询用户的信息，返回UserDetails
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name", username);
        User user = userDao.selectOne(queryWrapper);
        if(Objects.isNull(user)){
            throw new UsernameNotFoundException("用户不存在");
        }
//        封装用户的权限信息
        List<String> authorities = new ArrayList<>(Arrays.asList("test","admin"));
        return new LoginUser(user,authorities);
    }
}
